package com.gitee.feizns.explore.data_structure.tree.bs.mid;

/**
 * 129. 求根到叶子节点数字之和
 * @author feizns
 * @since 2020/3/9
 */
public class SumNumbers {

    public static int sumNumbers(TreeNode root) {
        return dfs(root, 0);
    }

    private static int dfs(TreeNode root, int val) {
        if ( root != null ) {
            int newVal = val * 10 + root.val;
            if ( root.left == null && root.right == null )
                return newVal;
            return dfs(root.left, newVal) + dfs(root.right, newVal);
        }
        return 0;
    }

}
